package nl.hva.web.mvcwebapp.controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import nl.hva.web.mvcwebapp.doa.CourseDAO;
import nl.hva.web.mvcwebapp.doa.ForumDAO;
import nl.hva.web.mvcwebapp.doa.NewsDAO;
import nl.hva.web.mvcwebapp.doa.UserDAO;
import nl.hva.web.mvcwebapp.models.ForumThread;
import nl.hva.web.mvcwebapp.models.ForumTopic;
import nl.hva.web.mvcwebapp.models.News;
import nl.hva.web.mvcwebapp.models.User;

public class AdminController extends HttpServlet {
    private int user_id, user_type;
    private String address = "/admin/admin_overview.jsp";
    ArrayList paths_jsp = new ArrayList();
    ArrayList paths = new ArrayList();

    /*
     * HTTP GET request
     */
    public ArrayList getPaths() {
        ArrayList paths = new ArrayList();
        paths.add(0, "/admin");

        return paths;
    }

    public ArrayList getPathsJSP() {
        ArrayList paths_jsp = new ArrayList();
        paths_jsp.add(0, "/admin/admin_overview.jsp");



        return paths_jsp;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        HttpSession httpsession = request.getSession(true);
        
        HttpSession session_user = request.getSession(true);
        user_id = Integer.parseInt(session_user.getAttribute("user_id").toString());
        user_type = Integer.parseInt(session_user.getAttribute("user_type").toString());
        request.setAttribute("currentUserType", user_type); 
        request.setAttribute("currentUserId", user_id);
        
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        boolean redirect = false;
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        String path = request.getServletPath();
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    // /course/catalog
                    if (i == 0) {
                        List<User> userlist = UserDAO.getAllUSers();
                        request.setAttribute("gebruikersUitSessie", userlist);
                        request.setAttribute("aantalGebruikers", userlist.size());
                        
                        
                        
                        List<Course> courselist = CourseDAO.getAllCourses();
                        request.setAttribute("courses", courselist);
                        request.setAttribute("amountcourses", courselist.size());
                        
                        List<News> nieuwslijst = NewsDAO.getAllNews();
                        request.setAttribute("nieuwsUitSessie", nieuwslijst);
                        request.setAttribute("aantalNieuws", nieuwslijst.size());
                        
                        List<ForumTopic> forums = ForumDAO.getAllForums();
                        request.setAttribute("forumsUitSessie", forums);
                        request.setAttribute("aantalForums", forums.size());

                        List<ForumThread> threads = ForumDAO.getAllThreads();
                        request.setAttribute("threadsUitSessie", threads);
                        request.setAttribute("aantalThreads", threads.size());
                        
                        
                        tx.commit();
                        
                    } // /course/view
                    else if (i == 1) {
                        
                    }// /course/edit
                    
                    address = paths_jsp.get(i).toString();
                }
            } catch (Exception E) {
            }
        }

        request.setAttribute("path", path);
        if (!redirect) {
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    address = paths_jsp.get(i).toString();
                    // ---------------- /course/update --------------------
                    if (i == 2) {
                        System.out.println("update");
                        int userid = 2;
                        User user = UserDAO.getUser(userid);
                        CourseDAO.updateCourse(request, user);
                        response.sendRedirect(response.encodeRedirectURL("../course/catalog"));
                    }// ---------------- /course/new --------------------
 
                }

            } catch (Exception E) {
            }
        }

    }
}
